package cn.edu.scau.disk.service.impl;

import cn.edu.scau.disk.exception.CommonException;
import cn.edu.scau.disk.mapper.UserMapper;
import cn.edu.scau.disk.pojo.User;
import cn.edu.scau.disk.service.TestService;
import cn.edu.scau.disk.utils.RedisConstant;
import cn.edu.scau.disk.utils.RedisUtil;
import cn.edu.scau.disk.utils.TokenGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public String login(User user) throws CommonException {
        User u = userMapper.selectOne(new QueryWrapper<User>()
                .eq("username",user.getUsername())
                .eq("password",user.getPassword()));

        if(u == null){
            throw new CommonException("用户名不存在或密码错误");
        }
        String token = TokenGenerator.generateValue();
        redisUtil.set(RedisConstant.TOKEN+token,String.valueOf(u.getId()));
        return token;
    }
}
